<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MVC.
 *
 * PHP-MVC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MVC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MVC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-mvc
 * @subpackage  util
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

class TagUtils {


    /**
     * Look up and return a message string, based on the specified
     * parameters.
     *
     * @param pageContext The PageContext associated with this request
     * @param bundle      Name of the servlet context attribute for our
     *                    message resources bundle
     * @param locale      Name of the session attribute for our user's Locale
     * @param key         Message key to be looked up and returned
     * @param args        Replacement parameters for this message
     * @return message string
     * @throws JspException if a lookup error occurs (will have been saved in
     *                      the request already)
     */
    public static function message($pageContext, $bundle, $locale, $key, $args) {

        $log =& LoggerManager::getLogger("TagUtils");

        $resources =
            self::retrieveMessageResources($pageContext, $bundle);

        $userLocale = self::getUserLocale($pageContext, $locale);

        $message = null;

        if ($args === null) {
            $message = $resources->getMessage($userLocale, $key);
        } else {
            $message = $resources->getMessage($userLocale, $key, $args);
        }


        if (($message === null) && $log->isDebugEnabled()) {
            // log missing key to ease debugging
            $log->debug("Missing key : " . $key . " : bundle " . $bundle . " : locale " . $locale);
        }

        return $message;
    }


    /**
     * Returns the appropriate MessageResources object for the current module
     * and the given bundle.
     *
     * @param pageContext    Search the context's scopes for the resources.
     * @param bundle         The bundle name to look for.  If this is
     *                       <code>null</code>, the default bundle name is
     *                       used.
     * @param checkPageScope Whether to check page scope
     * @return MessageResources The bundle's resources stored in some scope.
     * @throws JspException if the MessageResources object could not be
     *                      found.
     */
    public function retrieveMessageResources($pageContext, $bundle) {

        $resources = null;

        if ($bundle === null) {
            $bundle = MVC::MESSAGES_KEY;
        }

        $moduleConfig = $pageContext->moduleConfig;

        $resources =
            $pageContext->applicationContext->getAttribute($bundle
                . $moduleConfig->getPrefix());

        if ($resources === null) {
            $resources =
                $pageContext->applicationContext->getAttribute($bundle);
        }

        if ($resources === null) {
            throw new Exception("No bundle '".$bundle."' could be found");
        }

        return $resources;
    }


    /**
     * Look up and return current user locale, based on the specified
     * parameters.
     *
     * @param pageContext The PageContext associated with this request
     * @param locale      Name of the session attribute for our user's Locale.
     *                    If this is <code>null</code>, the default locale key
     *                    is used for the lookup.
     * @return current user locale
     */
    public function getUserLocale($pageContext, $locale) {
        return RequestUtils::getUserLocale($pageContext->request, $locale);
    }

}

?>